﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace APPLICATION.UserManager
{
    public partial class FrmAddEditUser : Form
    {

        public FrmAddEditUser()
        {
            InitializeComponent();
            
        }

        public bool ForEditData;
        public int UserID;
        public int StaffID;
        private bool PasswordChanged;
        //When data is set on the radioButton control and this data value is equal null,  then we get error !! IT IS BUG I think
        private object _radioButtonMaleDataValue;
        public object radioButtonDataValue
        {
            get
            {
                return _radioButtonMaleDataValue;
            }
            set
            {
                _radioButtonMaleDataValue = value;
                if (_radioButtonMaleDataValue == DBNull.Value)
                {
                    radioButtonMale.Checked = true;
                    return;
                }
                if (Convert.ToBoolean(_radioButtonMaleDataValue) == true)
                {
                    radioButtonMale.Checked = true;
                    radioButtonFemale.Checked = false;
                }
                else
                {
                    radioButtonMale.Checked = false;
                    radioButtonFemale.Checked = true;
                }
            }
        }


        private bool Checked()
        {
            if (txtUserName.isEmpty)
            {
                MessageBox.Show(this, "Ведитье логин пользователя","User Manager",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                txtUserName.Focus();                
                return false;
            }
            if(textBoxPassword.Text != textBoxPassword1.Text)
            {
                MessageBox.Show(this, "Ведитье одинаковие пароли", "User Manager", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBoxPassword1.Focus();
                textBoxPassword1.SelectAll();
                return false;
            }
            if (string.IsNullOrEmpty(textBoxStaffName.Text))
            {
                MessageBox.Show(this, "Ведитье название ползобателя", "User Manager", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBoxStaffName.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(choiserDep.TextBoxSearch.Text))
            {
                MessageBox.Show(this, "Виберите департамент", "User Manager", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                choiserDep.TextBoxSearch.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(choiserPost.TextBoxSearch.Text))
            {
                MessageBox.Show(this, "Виберите пост ползобателя", "User Manager", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                choiserPost.TextBoxSearch.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(choiserCity.TextBoxSearch.Text))
            {
                MessageBox.Show(this, "Виберите город ползобателя", "User Manager", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                choiserCity.TextBoxSearch.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(calendarTextBoxDborthd.Text))
            {
                MessageBox.Show(this, "Ведитье дата рождения", "User Manager", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                calendarTextBoxDborthd.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(calendarTextBoxStartWork.Text))
            {
                MessageBox.Show(this, "Ведитье дата начало работы", "User Manager", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                calendarTextBoxStartWork.Focus();
                return false;
            }


            if (string.IsNullOrEmpty(textBoxPassportN.Text))
            {
                MessageBox.Show(this, "Ведитье номер паспорта", "User Manager", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBoxPassportN.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(textBoxTab.Text))
            {
                MessageBox.Show(this, "Ведитье табельный номер", "User Manager", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBoxTab.Focus();
                return false;
            }


            return true;
        }

        private bool Save()
        {
            UMdataAccess DataAccess = new UMdataAccess();
            if (Checked() == false)
            {
                return false;
            }


            #region Fill sp parameters for insert or update
            SqlParameter[] SqlParams = new SqlParameter[16];

            SqlParams[0] = new SqlParameter();
            SqlParams[0].ParameterName = "@Action";
            SqlParams[0].SqlDbType = SqlDbType.TinyInt;

            SqlParams[1] = new SqlParameter();
            SqlParams[1].ParameterName = "@UserN";
            SqlParams[1].SqlDbType = SqlDbType.VarChar;
            SqlParams[1].Size = 20;
            SqlParams[1].Value = txtUserName.Text;

            SqlParams[2] = new SqlParameter();
            SqlParams[2].ParameterName = "@Description";
            SqlParams[2].SqlDbType = SqlDbType.NVarChar;
            SqlParams[2].Size = 100;
            SqlParams[2].Value = txtDescription.Text;

            SqlParams[3] = new SqlParameter();
            SqlParams[3].ParameterName = "@Psw";
            SqlParams[3].SqlDbType = SqlDbType.VarBinary;
            SqlParams[3].Value = Encoding.Unicode.GetBytes(txtPassword.Text);

            SqlParams[4] = new SqlParameter();
            SqlParams[4].ParameterName = "@Spend";
            SqlParams[4].SqlDbType = SqlDbType.Bit;
            SqlParams[4].Value = Convert.ToInt16(chekBoxStatus.Checked);

            SqlParams[5] = new SqlParameter("@UserID", SqlDbType.Int);
            SqlParams[5].Direction = ParameterDirection.InputOutput;


            SqlParams[6] = new SqlParameter("@StaffID",SqlDbType.Int);
            SqlParams[6].Direction = ParameterDirection.InputOutput;

            SqlParams[7] = new SqlParameter("@DepID", SqlDbType.Int);
            SqlParams[7].Value = choiserDep.ResultManager.GetResult(choiserDep.Result, "CodeID");

            SqlParams[8] = new SqlParameter("@PostID", SqlDbType.Int);
            SqlParams[8].Value = choiserPost.ResultManager.GetResult(choiserPost.Result, "CodeID");

            SqlParams[9] = new SqlParameter("@CityID", SqlDbType.Int);
            SqlParams[9].Value = choiserCity.ResultManager.GetResult(choiserCity.Result, "CodeID");

            SqlParams[10] = new SqlParameter("@Gender", SqlDbType.Bit);
            SqlParams[10].Value = radioButtonMale.Checked;

            SqlParams[11] = new SqlParameter("@DBorn", SqlDbType.SmallDateTime);
            SqlParams[11].Value =DateTime.Parse(calendarTextBoxDborthd.Text);

            SqlParams[12] = new SqlParameter("@DWork", SqlDbType.SmallDateTime);
            SqlParams[12].Value =DateTime.Parse(calendarTextBoxStartWork.Text);

            
            SqlParams[13] = new SqlParameter("@PerNum", SqlDbType.NVarChar,12);
            SqlParams[13].Value = textBoxPassportN.Text;

            SqlParams[14] = new SqlParameter("@TabNum", SqlDbType.Int);
            SqlParams[14].Value = textBoxTab.Text;

            SqlParams[15] = new SqlParameter("@StaffName", SqlDbType.NVarChar,100);
            SqlParams[15].Value = textBoxStaffName.Text;
            #endregion

            if (ForEditData)
            {
                //@Action
                SqlParams[0].Value = 1;
                //@UserID
                SqlParams[5].Value = UserID;
                //@StaffID
                SqlParams[6].Value = StaffID;
                if (DataAccess.addOrUpdUser(SqlParams,bindingSourceUser,bindingSourceStaff))
                { 
                    return true;
                }
            }
            else
            {
                //@Action
                SqlParams[0].Value = 0;
                //@UserID
                SqlParams[5].Value = DBNull.Value;
                //@StaffID
                SqlParams[6].Value = DBNull.Value;
                if (DataAccess.addOrUpdUser(SqlParams, bindingSourceUser, bindingSourceStaff))
                {
                    return true;
                }

            }
            return false;
        }

        private bool AddNewUser()
        {
            ForEditData = false;
            bindingSourceUser.AddNew();                
            bindingSourceStaff.AddNew();
            textBoxPassword1.Text = "";
            textBoxPassword.Text = "";
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Save())
            {
                this.Close();
            }
        }
        
        private void FrmAddEditUsers_Load(object sender, EventArgs e)
        {
            choiserDep.ConnectionString = globals.APPCONNSTRING;

            DataView dwUser= new DataView(GlobalDataFile.UserManagerDataSet.stUsers);
            bindingSourceUser.DataSource = dwUser;

            DataView dwStaff = new DataView(GlobalDataFile.UserManagerDataSet.dtStaff);
            bindingSourceStaff.DataSource = dwStaff;

            txtUserName.DataBindings.Add("Text", bindingSourceUser, "UserN");
            chekBoxStatus.DataBindings.Add("Checked", bindingSourceUser, "Spend");
            txtDescription.DataBindings.Add("Text", bindingSourceUser, "Description");

            textBoxStaffName.DataBindings.Add("Text", bindingSourceStaff, "StaffName");
            choiserDep.DataBindings.Add("IdentityValue", bindingSourceStaff, "DepID");
            choiserPost.DataBindings.Add("IdentityValue", bindingSourceStaff, "PostID");
            choiserCity.DataBindings.Add("IdentityValue", bindingSourceStaff, "CityID");

            this.DataBindings.Add("radioButtonDataValue", bindingSourceStaff, "Gender");
            calendarTextBoxDborthd.DataBindings.Add("Text", bindingSourceStaff, "DBorn");
            calendarTextBoxStartWork.DataBindings.Add("Text", bindingSourceStaff, "DWork");
            textBoxPassportN.DataBindings.Add("Text", bindingSourceStaff, "PerNum");
            textBoxTab.DataBindings.Add("Text", bindingSourceStaff, "TabNum");
            

            if (ForEditData)
            {
                bindingSourceUser.Filter = "UserID=" + UserID.ToString();
                bindingSourceStaff.Filter = "StaffID=" + StaffID.ToString();
            }
            else
            {
                this.AddNewUser();
            }           
        }

        private void BtnSaveAndNew_Click(object sender, EventArgs e)
        {
            if (Save())
            {
                this.AddNewUser();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmAddEditUsers_FormClosing(object sender, FormClosingEventArgs e)
        {
            bindingSourceUser.CancelEdit();
            bindingSourceStaff.CancelEdit();
        }

        private void txtPassword_Properties_Modified(object sender, EventArgs e)
        {
            PasswordChanged = true;
        }

        private void label15_Click(object sender, EventArgs e)
        {

        }

        private void textBoxTab_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (textBoxTab.Text.Length > 7)
            {
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            } 

            
            
        }

    }
}
